

package persistencia;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class BaseDAO {

    private static Connection conexao = null;

    public BaseDAO() throws IOException {
        if(conexao == null) {
            try {
                Class.forName("org.postgresql.Driver");
                conexao = DriverManager.getConnection("jdbc:postgresql:postgres", "postgres", "postgres");
            } catch(ClassNotFoundException e) {
                throw new IOException("Driver PostgreSQL nao encontrado!");

            } catch(SQLException e) {
                throw new IOException("Nao foi possivel conectar ao banco: " + e.getMessage());
            }
        }
    }

    public Connection getConexao() {
        return conexao;
    }

    public void finalize() {
        try {
            conexao.close();
        } catch(Exception e) {}
    }
}
